#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import time
from engine.common import *
from shutil import copyfile
if sys.platform == "darwin":
    adbpath = "adb "
else:
    adbpath = "/usr/bin/adb "
if (sys.argv.__len__())<7:
    print( "参数个数少了")
    exit(0)
handset = sys.argv[1]
currentHandsetSnumber = sys.argv[2]
command = sys.argv[3]
resultF = sys.argv[4]

filePathLoc = sys.argv[5]
tempFile = sys.argv[6]

# 11.淘宝联盟点击复制口令保存好口令并转化保存
# 之前界面:淘宝联盟好物圈页面
# 期待界面:淘口令复制后界面
# 事前计算:复制淘口令
# 事前结果:posx,posy
# 动作执行:touchTap
# 事后计算:out*copyTKLAndSave*/tbk/alimama/*./temp/复制淘口令.txt
# 事后结果:./temp/复制淘口令.txt
# 出错状态转移码:None

def copyTKLAndSave( handset,currentHandsetSnumber,resultF,filePathLoc="/tbk/alimama/",tklFile="./temp/复制淘口令.txt",delay=5 ):
    ret = False
    setHandsetType(handset)
    setHandsetSno(currentHandsetSnumber)
    content = getAndSetMsgFromHandsetCliper("tkl",currentHandsetSnumber)
    content=content.replace("￥",'')
    content = '--------------[nr]{['+content+']}复制这条信息，打开【Táo宝】领卷下单[玫瑰]'
    if not os.path.exists(getHandsetFileSavePath(filePathLoc)):
        os.makedirs(getHandsetFileSavePath(filePathLoc))
    with open(getHandsetFileSavePath(filePathLoc) + "/curmd5.txt") as f:
        curdirmd5 = f.read()


        #create it now and
    #os.makedirs(basePath + resultF+curdirmd5+"/")
    with open(getHandsetFileSavePath(filePathLoc)+curdirmd5+"/tkl.txt",'w') as f:
        print('wriete to file:'+content)
        f.write(content)
    with open(getHandsetFileSavePath(filePathLoc)+curdirmd5+"/tkl.txt.sh",'w') as f:
        print('wriete to file:'+content)
        #am broadcast -a clipper.get -e text "--------------[nr]{[cX3G19EJbUh]}复制这条信息，打开【Táo宝】查看[玫瑰]"
        shellContent = "adb -s "+currentHandsetSnumber+"  shell  am broadcast -a clipper.get  -f 0x01000000 -e text \""+content+"\""
        f.write(shellContent)

    #adb pull files to this dir and save
    #联盟图片目录在  adb pull /storage/emulated/0/Pictures/淘宝联盟 temp pull到temp目录到 淘宝联盟
    AdbPullFromHandset("/storage/emulated/0/Pictures/淘宝联盟  "+getHandsetFileSavePath(""),currentHandsetSnumber)
    # AdbShellRmFromHandset("/storage/emulated/0/Pictures/淘宝联盟/*.*",currentHandsetSnumber)

    if os.path.exists(getHandsetFileSavePath("淘宝联盟")):
        print(getHandsetFileSavePath("淘宝联盟") +getHandsetFileSavePath(filePathLoc)+curdirmd5+"/")
        cmd = "cp "+getHandsetFileSavePath("淘宝联盟")+"/* "+getHandsetFileSavePath(filePathLoc)+curdirmd5+"/"
        os.system(cmd)
        print("9sec will del files ")
        time.sleep(9)
        cmd = "rm -rf " + getHandsetFileSavePath("淘宝联盟")
        os.system(cmd)

    with open(getHandsetFileSavePath(tklFile),'w') as f:
        print('wriete to file')
        f.write(content)


    with open(getHandsetFileSavePath(resultF),'w')as f:
        if ret == True:
            f.write('True')
            return True  #TRUE 状态转移
        else:
            f.write('False')
            return False


if command =="copyTKLAndSave".strip():
    copyTKLAndSave(handset,currentHandsetSnumber,resultF,filePathLoc,tempFile)
